// pages/signInShop/signInShop.js
import __config from '../../../config/env.js'
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk')
var qqmapsdk = new QQMapWX({
  key: 'VYSBZ-K273P-GEQD3-LLP4A-KATX2-SIBXR' // 必填
})
import validate from '../../../utils/validate.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:__config.basePath,
    storeid:'',
    jinDu:0,
    weiDu:0,
    dis:'',
    numList: [{
      name: '签到'
    }, {
      name: '拍照'
    }, {
      name: '签退'
    }],
    camaraType:'',
    num:0,
    shop:{},
    src:{},
    signInimage:'',
    signOutimage:'',

    shelfimage1:'',
    shelfimage2:'',
    shelfimage3:'',

    stackimage1:'',
    stackimage2:'',
    stackimage3:'',

    advertsimage1:'',
    advertsimage2:'',
    advertsimage3:'',

    posterimage1:'',
    posterimage2:'',
    posterimage3:'',

    TabCur:0,
    myLocation:'',
    itude:'',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyLocation();
    this.setData({
      storeid:options.storeid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })
      }
    })
  },
  imgUpload(url){
    if(this.data.camaraType == 'signInimage'){
      this.setData({
        signInimage:url
      })
    }else if(this.data.camaraType == 'shelfimage1'){
        this.setData({
          shelfimage1:url
      })
    }else if(this.data.camaraType == 'shelfimage2'){
        this.setData({
          shelfimage2:url
      })
    }else if(this.data.camaraType == 'shelfimage3'){
      this.setData({
        shelfimage3:url
    })
    }else if(this.data.camaraType == 'stackimage1'){
        this.setData({
          stackimage1:url
      })
    }else if(this.data.camaraType == 'stackimage2'){
      this.setData({
        stackimage2:url
    })
    }else if(this.data.camaraType == 'stackimage3'){
      this.setData({
        stackimage3:url
    })
    }else if(this.data.camaraType == 'advertsimage1'){
      this.setData({
        advertsimage1:url
    })
    }else if(this.data.camaraType == 'advertsimage2'){
        this.setData({
          advertsimage2:url
      })
    }else if(this.data.camaraType == 'advertsimage3'){
      this.setData({
        advertsimage3:url
    })
    }else if(this.data.camaraType == 'posterimage1'){
        this.setData({
          posterimage1:url
        })
    }else if(this.data.camaraType == 'posterimage2'){
      this.setData({
        posterimage2:url
      })
    }else if(this.data.camaraType == 'posterimage3'){
      this.setData({
        posterimage3:url
      })
    }else if(this.data.camaraType == 'signOutimage'){
      this.setData({
        signOutimage:url
      })
    }
  },
  getMyLocation(){
    let that = this;
    wx.getLocation({
      type: "gcj02",
      success: res => {
        console.log(res)
        let jinDu = res.latitude //经度
        let weiDu = res.longitude //纬度

        let obj = {latitude:jinDu,longitude:weiDu}
        this.setData({
          itude:JSON.stringify(obj),
          jinDu:res.latitude,
          weiDu:res.longitude
        })
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: jinDu,
            longitude: weiDu
          },
          success: (res) => {
            that.setData({
              myLocation: res.result.address
            })
          }
        })
      },
      complete: res => {
        this.getShopDetail();
      }
      //
    })
  },
  getShopDetail(){
    app.api.getShopDetail(this.data.storeid).then(res =>{
      console.log(res)
      this.setData({
        shop:res.rows
      })
      try{
        let itu = JSON.parse(res.rows.itude)
        let dis = this.getDisance(itu.latitude,itu.longitude,this.data.jinDu,this.data.weiDu)
        this.setData({
          dis:dis
        })
      }catch(e){
        console.log(e)
      }
      this.setData({
        signInimage:res.rows.signimage1 || '',
        signOutimage:res.rows.signoutimage1 || '',

        shelfimage1:res.rows.shelfimage1 || '',
        shelfimage2:res.rows.shelfimage2 || '',
        shelfimage3:res.rows.shelfimage3 || '',

        stackimage1:res.rows.stackimage1 || '',
        stackimage2:res.rows.stackimage2 || '',
        stackimage3:res.rows.stackimage3 || '',

        advertsimage1:res.rows.advertsimage1 || '',
        advertsimage2:res.rows.advertsimage2 || '',
        advertsimage3:res.rows.advertsimage3 || '',

        posterimage1:res.rows.posterimage1 || '',
        posterimage2:res.rows.posterimage2 || '',
        posterimage3:res.rows.posterimage3 || '',
      })
      if(res.rows.progress == '0'){
        this.setData({
          num:1
        })
      }else if(res.rows.progress == '1'){
        this.setData({
          num:2
        })
      }else{
        this.setData({
          num:0
        })
      }
    })
  },
  openCanmera(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      camaraType:type
    })
    wx.navigateTo({
      url: '/pages/takePhoto/takePhoto'
    })
  },
  addImg(img){
    this.imgUpload(img)
  },
  changeStep(e){
    let teb = e.currentTarget.dataset.index-1
    // if(teb <= this.data.shop.progress+1){
    //   this.setData({
    //     num:teb
    //   })
    // }
  },
  DelImg(e){
    let type = e.currentTarget.dataset.type
    if(type== 'signInimage'){
      this.setData({
        signInimage:''
      })
    }
    else if(type == 'shelfimage1'){
      this.setData({
        shelfimage1:''
      })
    }
    else if(type == 'shelfimage2'){
      this.setData({
        shelfimage2:''
      })
    }
    else if(type == 'shelfimage3'){
      this.setData({
        shelfimage3:''
      })
    }else if(type == 'stackimage1'){
        this.setData({
          stackimage1:''
      })
    }
    else if(type == 'stackimage2'){
      this.setData({
        stackimage2:''
      })
    }
    else if(type == 'stackimage3'){
      this.setData({
        stackimage3:''
      })
    }
    else if(type == 'advertsimage1'){
      this.setData({
        advertsimage1:''
      })
    }else if(type == 'advertsimage2'){
        this.setData({
          advertsimage2:''
      })
    }
    else if(type == 'advertsimage3'){
      this.setData({
        advertsimage3:''
      })
    }
    else if(this.data.camaraType == 'posterimage1'){
      this.setData({
        posterimage1:''
      })
    }
    else if(this.data.camaraType == 'posterimage2'){
      this.setData({
        posterimage2:''
      })
    }
    else if(this.data.camaraType == 'posterimage3'){
      this.setData({
        posterimage3:''
      })
    }
    else if(this.data.camaraType == 'signOutimage'){
      this.setData({
        signOutimage:''
      })
    }
  },
  signIn(){
    let obj = {}
    if(this.data.jinDu == 0){
      wx.showModal({
        title: '提示',
        content: '获取经纬度失败',
        showCancel:false,
        success:res =>{
          wx.navigateBack()
        }
      })
    }
    else if(validate.validatenull(this.data.signInimage)){
      wx.showModal({
        title: '提示',
        content: '请拍摄签到照片',
        showCancel:false,
        success:res =>{
          wx.navigateBack()
        }
      })
    }
    else {
      obj.gid = this.data.shop.gid
      obj.itude = this.data.itude
      obj.address = this.data.myLocation
      app.api.imgUpload(this.data.signInimage).then(res => {
        obj.imageurl = res.imageurl
        app.api.saveSignin(obj).then(res =>{
          if(res.code == 200){
            app.dialog.success(res.info)
            this.getShopDetail()
          }
        })
      })
    }
  },
  savePaizhaoimage(){
    if(this.data.jinDu == 0){
      wx.showModal({
        title: '提示',
        content: '获取经纬度失败',
        showCancel:false,
        success:res =>{
          wx.navigateBack()
        }
      })
    }else if(this.data.shelfimage1 == '' && this.data.shelfimage2  == '' &&  this.data.shelfimage3 == ''){
      wx.showModal({
        title: '提示',
        content: '货架照片至少拍摄一张',
        showCancel:false
      })
    }
    else if(this.data.stackimage1 == '' && this.data.stackimage2 == '' && this.data.stackimage3 == ''){
      wx.showModal({
        title: '提示',
        content: '堆头照片至少拍摄一张',
        showCancel:false
      })
    }else{
        
        if(this.data.shelfimage1 == '' &&  this.data.shelfimage2 != ''){
          this.data.shelfimage1 = this.data.shelfimage2;
          this.data.shelfimage2 = ''
        }else if(this.data.shelfimage1 == '' &&  this.data.shelfimage3 != ''){
          this.data.shelfimage1 = this.data.shelfimage3;
          this.data.shelfimage3 = ''
        }
        if(this.data.stackimage1 == '' &&  this.data.stackimage2 != ''){
          this.data.stackimage1 = this.data.stackimage2;
          this.data.stackimage2 = ''
        }else if(this.data.stackimage1 == '' &&  this.data.stackimage3 != ''){
          this.data.stackimage1 =  this.data.stackimage3;
          this.data.stackimage3 = ''
        }
        this.forUpload('shelfimage1')
    }
  },
  forUpload(type){
    if(type == 'shelfimage1'){
      if(this.data.shelfimage1 == ''){
        this.forUpload('shelfimage2')
        this.setData({
          'shop.shelfimage' : ''
        })
      }else{
        app.api.imgUpload(this.data.shelfimage1).then(res => {
          this.setData({
            'shop.shelfimage' : res.imageurl
          })
          this.forUpload('shelfimage2')
        })
      }
    }
    else if(type == 'shelfimage2'){
      if(this.data.shelfimage2 == ''){
        this.forUpload('shelfimage3')
        this.setData({
          'shop.shelfimage2' : ''
        })
      }else{
        app.api.imgUpload(this.data.shelfimage2).then(res => {
          this.setData({
            'shop.shelfimage2' : res.imageurl
          })
          this.forUpload('shelfimage3')
        })
      }
    }
    else if(type == 'shelfimage3'){
      if(this.data.shelfimage3 == ''){
        this.forUpload('stackimage1')
        this.setData({
          'shop.shelfimage3' : ''
        })
      }else{
        app.api.imgUpload(this.data.shelfimage3).then(res => {
          this.setData({
            'shop.shelfimage3' : res.imageurl
          })
          this.forUpload('stackimage1')
        })
      }
    }


    else if(type == 'stackimage1'){
      if(this.data.stackimage1 == ''){
        this.forUpload('stackimage2')
        this.setData({
          'shop.stackimage1' : ''
        })
      }else{
        app.api.imgUpload(this.data.stackimage1).then(res => {
          this.setData({
            'shop.stackimage1' : res.imageurl
          })
          this.forUpload('stackimage2')
        })
      }
    }
    else if(type == 'stackimage2'){
      if(this.data.stackimage2 == ''){
        this.forUpload('stackimage3')
        this.setData({
          'shop.stackimage2' : ''
        })
      }else{
        app.api.imgUpload(this.data.stackimage2).then(res => {
          this.setData({
            'shop.stackimage2' : res.imageurl
          })
          this.forUpload('stackimage3')
        })
      }
    }
    else if(type == 'stackimage3'){
      if(this.data.stackimage3 == ''){
        this.forUpload('advertsimage1')
        this.setData({
          'shop.stackimage3' : ''
        })
      }else{
        app.api.imgUpload(this.data.stackimage3).then(res => {
          this.setData({
            'shop.stackimage3' : res.imageurl
          })
          this.forUpload('advertsimage1')
        })
      }
    }
    else if(type == 'advertsimage1'){
      if(this.data.advertsimage1 == ''){
        this.forUpload('advertsimage2')
        this.setData({
          'shop.advertsimage1' : ''
        })
      }else{
        app.api.imgUpload(this.data.advertsimage1).then(res => {
          this.setData({
            'shop.advertsimage1' : res.imageurl
          })
          this.forUpload('advertsimage2')
        })
      }
    }
    else if(type == 'advertsimage2'){
      if(this.data.advertsimage2 == ''){
        this.forUpload('advertsimage3')
        this.setData({
          'shop.advertsimage2' : ''
        })
      }else{
        app.api.imgUpload(this.data.advertsimage2).then(res => {
          this.setData({
            'shop.advertsimage2' : res.imageurl
          })
          this.forUpload('advertsimage3')
        })
      }
    }
    else if(type == 'advertsimage3'){
      if(this.data.advertsimage3 == ''){
        this.forUpload('posterimage1')
        this.setData({
          'shop.advertsimage3' : ''
        })
      }else{
        app.api.imgUpload(this.data.advertsimage3).then(res => {
          this.setData({
            'shop.advertsimage3' : res.imageurl
          })
          this.forUpload('posterimage1')
        })
      }
    }

    else if(type == 'posterimage1'){
      if(this.data.posterimage1 == ''){
        this.forUpload('posterimage2')
        this.setData({
          'shop.posterimage1' : ''
        })
      }else{
        app.api.imgUpload(this.data.posterimage1).then(res => {
          this.setData({
            'shop.posterimage1' : res.imageurl
          })
          this.forUpload('posterimage2')
        })
      }
    }
    else if(type == 'posterimage2'){
      if(this.data.posterimage2 == ''){
        this.forUpload('posterimage3')
        this.setData({
          'shop.posterimage2' : ''
        })
      }else{
        app.api.imgUpload(this.data.posterimage2).then(res => {
          this.setData({
            'shop.posterimage2' : res.imageurl
          })
          this.forUpload('posterimage3')
        })
      }
    }
    else if(type == 'posterimage3'){
      if(this.data.posterimage3 == ''){
        this.setData({
          'shop.posterimage3' : ''
        })
        this.forUpload('save')
      }else{
        app.api.imgUpload(this.data.posterimage3).then(res => {
          this.setData({
            'shop.posterimage3' : res.imageurl
          })
          this.forUpload('save')
        })
      }
    }else if(type == 'save'){
      this.data.shop.shelfimage = this.data.shop.shelfimage1
      this.data.shop.stackimage = this.data.shop.stackimage1
      this.data.shop.advertsimage = this.data.shop.advertsimage1
      this.data.shop.posterimage = this.data.shop.posterimage1
      app.api.savePaizhaoimage(this.data.shop).then(res =>{
        if(res.code == 200){
          app.dialog.success(res.info)
          this.getShopDetail()
        }
      })
    }
  },
  signOut(){
    if(this.data.jinDu == 0){
      app.dialog.failLocation()
      return;
    }
    else if(validate.validatenull(this.data.signInimage)){
      wx.showModal({
        title: '提示',
        content: '请拍摄签到照片',
        showCancel:false,
        success:res =>{
          wx.navigateBack()
        }
      })
    }else{
      let obj = {}
      obj.gid = this.data.shop.gid
      obj.itude = this.data.itude
      obj.address = this.data.myLocation
      app.api.imgUpload(this.data.signOutimage).then(res => {
        obj.imageurl = res.imageurl
        app.api.saveSignout(obj).then(res =>{
          if(res.code == 200){
            app.dialog.success(res.info)
            wx.navigateBack();
          }
        })
      })
    }
  },
  ViewImage(e){
    let url = e.currentTarget.dataset.url
    console.log(url)
    wx.previewImage({
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  getDisance(la1, lo1, la2, lo2) {
    let La1 = la1 * Math.PI / 180.0;
    let La2 = la2 * Math.PI / 180.0;
    let La3 = La1 - La2;
    let Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  },
})